# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _
import time


def bmx_request_method(fapiao):
    print('bmx_request_method', fapiao)
    time.sleep(3)
    result = [{'id': one.id, 'serial_num': 'bmx_' + str(one.id)}
              for one in fapiao]

    for rec in result:
        one = fapiao.browse(rec['id'])
        serial_num = rec['serial_num']

        one.write({'bmx_serial_num': serial_num})

    return result


def bmx_query_method(fapiao):
    print('bmx_query_method', fapiao)
    time.sleep(3)
    return [{
        'id': one.id,
        'state': 'done',

        'taxmachine_code': one.taxmachine_code,
        'payee_name': one.payee_name,
        'checker_name': one.checker_name,
        'drawer_name': one.drawer_name,

        'date_fapiao': one.fapiao_id.date_order,
        'type_code': 'type_code_virtual' + str(one.id),
        'code': 'code_virtual' + str(one.id),
        'check_code': 'check_code_virtual' + str(one.id),

        'amount_untaxed': one.fapiao_id.amount_untaxed,
        'amount_tax': one.fapiao_id.amount_tax,
        'amount_total': one.fapiao_id.amount_total,
        'line_ids': [{
            # TODO 区分 折扣行和 普通行
            'id': line.id,
            'tax_ratio': line.tax_ratio,
            'quantity': line.quantity,
            'amount_untaxed': line.amount_untaxed,
            'amount_tax': line.amount_tax,
            'amount_total': line.amount_total,
            'price_untax': line.price_untax,
            'price_unit': line.price_unit,
        } for line in one.fapiao_id.line_ids
        ]

    } for one in fapiao]


def bmx_print_method(fapiao):
    print('bmx_print_method', fapiao)
    time.sleep(3)
    return [{'id': one.id} for one in fapiao]


class FapiaoDrawProvider(models.Model):
    _inherit = "fp.draw.provider"

    def _request_methods(self):
        result = super()._request_methods()
        result2 = result.copy()
        result2.update({'bmx': bmx_request_method})
        return result2

    def _query_methods(self):
        result = super()._query_methods()
        result2 = result.copy()
        result2.update({'bmx': bmx_query_method})
        return result2

    def _print_methods(self):
        result = super()._print_methods()
        result2 = result.copy()
        result2.update({'bmx': bmx_print_method})
        return result2


class FapiaoDraw(models.Model):
    _inherit = "fp.fapiao.draw"

    bmx_serial_num = fields.Char(readonly=True,  help="开票请求后, 返回的序列号")

    def action_request(self):
        self2 = self
        if self[0].draw_provider_id.code == 'bmx':
            self2 = self.search([
                ('id', 'in', self.ids),
                ('state', '=', 'draft'),
                ('bmx_serial_num', '=', False)])
        super(FapiaoDraw, self2).action_request()

    def action_query(self):
        self2 = self
        if self[0].draw_provider_id.code == 'bmx':
            self2 = self.search(
                [('id', 'in', self.ids),
                 ('state', 'in', ['requested', 'reread']),
                    ('bmx_serial_num', '!=', False)])

        super(FapiaoDraw, self2).action_query()
